package com.skyhub.mapper;

import com.skyhub.common.domain.dto.HeatMap;
import com.skyhub.common.domain.pojo.*;
import com.skyhub.common.domain.vo.UserThumbsUpRecord;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface HomeMapper {

    public List<Article> getArticleList();

    @Select("select user_id from article_series where id = #{seriesId}")
    public Integer GetUserIdBySeriesId(Article article);

    @Select("select * from users where id = #{id}")
    public User GetUserNameByUserId(User user);

    public Article getArticleById(Article article);

    public List<Article> getArticleBySeriesId(Article article);

    public List<SystemMessage> systemMessageList(SystemMessage systemMessage);

    public ArticleSeries getArticleSeriesById(ArticleSeries articleSeries);

    @Select("select * from article_series where user_id = #{userId}")
    public List<ArticleSeries> getArticleSeriesListByUserId(ArticleSeries series);

    @Delete("delete from articles where id = #{id}")
    public void DeleteArticleById(Article article);

    @Delete("delete from article_series where id = #{id}")
    public void DeleteArticleSeriesById(ArticleSeries series);

    @Delete("delete from articles where series_id = #{seriesId}")
    public void DeleteArticleBySeriesId(Article article);

    @Select("select count(*) from articles where series_id=#{seriesId}")
    public Integer GetArticleCountBySeriesId(Article article);

    @Update("update users set article_count=article_count-#{count} where id=#{userId}")
    public void subArticleCountById(@Param("count") Integer count,@Param("userId")Integer userId);

    public List<ArticleSeries>GetArticleSeriesListByUserId(ArticleSeries series);

    public List<Article>GetArticleListBySeriesId(@Param("list") List<Integer>list);

    @Select("select * from article_column")
    public List<ArticleColumn>getColumns();

    @Select("select * from article_cate")
    public List<ArticleColumn>getCateList();

    @Select("select * from user_heatmap where user_id = #{userId}")
    public List<HeatMap>heatMapListByUserId(HeatMap heatMap);

    @Update("update users set article_count=article_count+1 where id=#{userId};")
    public void addArticleCount(@Param("userId")Integer userId);

    @Update("update users set article_count=article_count-1 where id=#{userId};")
    public void subArticleCount(@Param("userId")Integer userId);

    @Update("update users set access_count=access_count+1 where id=(select user_id from article_series where id = (select series_id from articles where id = #{articleId}));")
    public void addAccessCount(@Param("articleId")Integer articleId);

    /**
     * 添加点赞数
     * @param articleId,userId
     */
    @Update("update articles set thumbs_up_count=thumbs_up_count+1 where id = #{articleId};")
    public void addThumbsUpCount(@Param("articleId")Integer articleId);

    @Insert("insert into user_thumbs_up_record(user_id,article_id) values(#{userId},#{articleId})")
    public void addThumbsUpRecord(@Param("userId")Integer userId,@Param("articleId")Integer articleId);

    @Update("update users set thumbs_up_count=thumbs_up_count+1 where id=(select user_id from skyhub.article_series where id=(select series_id from articles where id = #{articleId}))")
    public void addThumbsUpCountFromUser(@Param("articleId")Integer articleId);

    /**
     * 撤销点赞数
     * @param articleId,userId
     */
    @Update("update articles set thumbs_up_count=thumbs_up_count-1 where id = #{articleId}")
    public void removeThumbsUpCount(@Param("articleId")Integer articleId);

    @Insert("delete from user_thumbs_up_record where user_id=#{userId} and article_id=#{articleId}")
    public void removeThumbsUpRecord(@Param("userId")Integer userId,@Param("articleId")Integer articleId);

    @Update("update users set thumbs_up_count=thumbs_up_count-1 where id=(select user_id from skyhub.article_series where id=(select series_id from articles where id = #{articleId}))")
    public void removeThumbsUpCountFromUser(@Param("articleId")Integer articleId);

    @Select("select * from articles where id in (select article_id from user_thumbs_up_record where user_id = #{userId})")
    public List<Article>getRecords(@Param("userId")Integer userId);


    @Select("select * from articles order by thumbs_up_count desc limit 0,5")
    public List<Article>hotsArticle();
}
